package org.responseChain;

public abstract class Handler {
	private Handler nextHaddler;

	// 设置下一个处理者
	public void setNext(Handler _handler) {
		nextHaddler = _handler;
	}

	// 对上 层接口开放的方法
	public final Response handleMessage(Request request) {
		Response response = null;
		// 判断是否是自己处理的级别，是的话就进行处理
		if (this.getHandlerLevel().equals(request.getHandlerLevel())) {
			response = this.echo(request);
		} else {
			// 如果有下一节点，则交由下一节点处理
			if (this.nextHaddler != null) {
				response = nextHaddler.handleMessage(request);
			} else {
				System.out.println("找不到请求的处理者");
				System.out.println();
				System.out.println();
				System.out.println();
			}
		}
		return response;
	}

	// 子类实现，设置数据链子类的处理级别
	protected abstract String getHandlerLevel();

	// 子类实现的业务方法
	protected abstract Response echo(Request request);
}
